import re

from appium.webdriver.common.appiumby import AppiumBy
from appium.webdriver.webdriver import WebDriver
from ...constants.common_sequence import SWITCH_ENV
from ...page.pay_page.pay_out_page import PayOutPage


class OrderListPage(object):
    """
    订单菜单页
    """

    def __init__(self, client):
        self.client = client
        self.driver: WebDriver = client.driver

    """
    SubPage Button Function
    """

    def order_pay_detail_back(self):
        """
        订单支付详情后进行返回
        """
        self.driver.find_element(AppiumBy.ID, f'{SWITCH_ENV}:id/ll_print_ticket').click()

    def entire_back(self):
        """
        整单退菜
        """
        self.driver.find_element(AppiumBy.ID, f'{SWITCH_ENV}:id/tv_return_all').click()
        from cash_pos.page.order_page.entire_return_page import EntireReturnPage
        return EntireReturnPage(self.client)

    def part_back(self):
        """
        部分退菜
        """
        self.driver.find_element(AppiumBy.ID, f'{SWITCH_ENV}:id/tv_return_part').click()
        from cash_pos.page.order_page.part_return_page import PartReturnPage
        return PartReturnPage(self.client)

    def print_meal_label(self):
        """
        打印餐票
        """
        self.driver.find_element(AppiumBy.ID, f'{SWITCH_ENV}:id/ll_print_ticket').click()
        return self

    def print_single_meal_label(self):
        """
        打印单菜餐票
        """
        self.driver.find_element(AppiumBy.ID, f'{SWITCH_ENV}:id/ll_print_ticket_part').click()
        return self

    def print_small_label(self):
        """
        打印小票
        """
        self.driver.find_element(AppiumBy.ID, f'{SWITCH_ENV}:id/ll_print_receipts').click()
        return self

    def print_label(self):
        """
        打印标签
        """
        self.driver.find_element(AppiumBy.ID, f'{SWITCH_ENV}:id/ll_print_label').click()
        return self

    """
    Button Function
    """

    def order_pay_detail(self):
        """
        订单支付详情【在订单详情页点击VER】
        """
        self.driver.find_element(AppiumBy.ID, f'{SWITCH_ENV}:id/tv_pay_status').click()

    def pay_now(self):
        """
        去支付
        """
        self.driver.find_element(AppiumBy.ID, f'{SWITCH_ENV}:id/ll_conrar').click()
        return PayOutPage(self.client)

    def chargeback(self):
        """
        作废订单
        """
        self.driver.find_element(AppiumBy.ID, f'{SWITCH_ENV}:id/ll_print_return').click()
        return self

    def return_dishes(self):
        """
        退菜/退单
        """
        self.driver.find_element(AppiumBy.ID, f'{SWITCH_ENV}:id/ll_print_return').click()
        return self

    def print(self):
        """
        打印
        """
        self.driver.find_element(AppiumBy.ID, f'{SWITCH_ENV}:id/ll_print_reprint').click()
        return self

    """
    Service Function
    """

    def find_label_according_to_number(self, number):
        """
        根据流水号找到具体的单子
        """
        for i in range(20):
            container_element = self.driver.find_element(AppiumBy.ID, f'{SWITCH_ENV}:id/rl_left_view')
            from_element_location = re.match(r"\[(\d+,\d+)\]\[(\d+,\d+)\]", container_element.get_attribute("bounds")).group(1).split(',')
            to_element_location = re.match(r"\[(\d+,\d+)\]\[(\d+,\d+)\]", container_element.get_attribute("bounds")).group(2).split(',')
            from_x, from_y, to_x, to_y = int(from_element_location[0]), int(from_element_location[1]), int(to_element_location[0]), int(to_element_location[1])
            label_list = self.driver.find_elements(AppiumBy.XPATH, f'//android.widget.TextView[@resource-id="{SWITCH_ENV}:id/tv_order_row_no" and @text="{number}"]')
            if len(label_list) != 0:
                label_list[0].click()
                break
            else:
                self.driver.swipe(start_x=int((from_x + to_x) / 2), start_y=int((from_y + to_y) / 2 + 300), end_x=int((from_x + to_x) / 2), end_y=int((from_y + to_y) / 2), duration=200)

        return self

    def switch_order_tab(self, target:int):
        """
        在订单列表中切换订单号
        Params:
        -target:目标单号，这里只是索引
        """
        available_elements=self.driver.find_elements(AppiumBy.ID,f'{SWITCH_ENV}:id/cl_item_view')
        available_elements[target-1].click()
        return self
